package edu.neumont.pro180._2009_q2;

import java.awt.Point;

import edu.neumont.pro180._2009_q2.interfaces.ICommand;
import edu.neumont.pro180._2009_q2.interfaces.IGamePiece;

/**
 * Represents a Square on a Board.
 * 
 * @author Tyler Young
 */
public class Square
{
	// private Checker checker;
	private IGamePiece	gamePiece;
	private Board		owner;
	private int			x, y, whiteValue, blackValue, whiteDefValue, blackDefValue;

	/**
	 * Creates a Square that can be used on a Board.
	 * 
	 * @param owner
	 *            The Board that the Square will be on.
	 */
	public Square(Board owner, int x, int y)
	{
		this.x = x;
		this.y = y;
		this.owner = owner;
		whiteValue = 0;
		blackValue = 0;
		whiteDefValue = 0;
		blackDefValue = 0;

	}

	/**
	 * Whether a checker is currently on the Square.
	 * 
	 * @return true if there is a checker on the Square, false if there isn't.
	 */
	public boolean hasGamePiece()
	{
		return gamePiece != null;
	}

	/**
	 * Get the checker that's currently on the Square.
	 * 
	 * @return The checker that's on the Square, or null if there is none.
	 */
	public IGamePiece getGamePiece()
	{
		return gamePiece;
	}

	/**
	 * Place an IGamePiece on the Square.
	 * 
	 * @param gamePiece
	 *            The IGamePiece to place.
	 */
	public ICommand setGamePiece(IGamePiece gamePiece)
	{
		IGamePiece oldPiece = this.gamePiece;
		ICommand command = new EmptyCommand();
		if (oldPiece != gamePiece)
		{
			if (oldPiece != null)
			{
				// Break the relationship
				this.gamePiece = null;
				command = oldPiece.getMove(null);
			}
			this.gamePiece = gamePiece;
			if (this.gamePiece != null)
			{
				command = this.gamePiece.getMove(this);
			}
		}
		return command;
	}

	/**
	 * Returns the neighbor of the square, offset by the Point passed in; or
	 * null if there is no neighbor at that location.
	 * 
	 * @param offset
	 *            The Point representing the (x,y) distance to travel to the
	 *            neighbor.
	 * @return The neighboring Square, or null if the requested offset is not on
	 *         the board.
	 */
	public Square getNeighbor(Point offset)
	{
		return this.owner.getNeighbor(this, offset);
	}

	/**
	 * Returns the neighbor of the square, offset by the Point passed in; or
	 * itself if there is no neighbor at that location.
	 * 
	 * @param offset
	 *            The Point representing the (x,y) distance to travel to the
	 *            neighbor.
	 * @return The neighboring Square, or itself if the requested offset is not
	 *         on the board.
	 */
	public Square getNeighborOrSelf(Point offset)
	{
		Square neighbor = this.getNeighbor(offset);
		if (null == neighbor)
		{
			neighbor = this;
		}
		return neighbor;
	}

	/**
	 * Returns the (x,y) distance between this Square and the destination
	 * Square.
	 * 
	 * @param destination
	 *            The Square to get the distance to.
	 * @return The Point representing the (x,y) distance to the destination
	 *         Square.
	 */
	public Point getOffset(Square destination)
	{
		return this.owner.getOffset(this, destination);
	}

	@Override
	public String toString()
	{
		return "(" + x + "," + y + ")";
	}

	public void setOffenceWhiteValue(int value)
	{
		this.whiteValue = value;
	}

	public void addOffenceWhiteValue()
	{
		whiteValue += 1;
	}

	public int getOffenceWhiteValue()
	{
		return whiteValue;
	}

	public void setOffenceBlackValue(int blackValue)
	{
		this.blackValue = blackValue;
	}

	public void addOffenceBlackValue()
	{
		blackValue += 1;
	}

	public int getOffenceBlackValue()
	{
		return blackValue;
	}

	public int getWhiteDefValue()
	{
		return whiteDefValue;
	}

	public void setWhiteDefValue(int whiteDefValue)
	{
		this.whiteDefValue = whiteDefValue;
	}

	public void addDefenciveWhiteValue()
	{
		whiteDefValue += 1;
	}

	public int getBlackDefVal()
	{
		return blackDefValue;
	}

	public void addDefenciveBlackValue()
	{
		blackDefValue += 1;
	}

	public void setBlackDefVal(int blackDefVal)
	{
		this.blackDefValue = blackDefVal;
	}
}
